home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / mega src / Source / man.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-13  |  3.4 KB  |  153 lines  |  [TEXT/KAHL]

  1. /* ========== the commmand file: ==========
  2.  
  3.     man.c - where normal Macintosh stuff is handled.
  4.     
  5.     Copyright (c) 1993,1994 Newport Software Development
  6.     
  7.     You may distribute unmodified copies of this file for
  8.     noncommercial purposes.  You may use this file as a
  9.     reference when writing your own nShell(tm) commands.
  10.     
  11.     All other rights are reserved.
  12.     
  13.    ========== the commmand file: ========== */
  14.  
  15. #include "nshc.h"
  16.                     
  17. #include "str_utl.proto.h"
  18. #include "nshc_utl.proto.h"
  19.  
  20. extern short ResErr;
  21.  
  22. /* ========== Utility Functions. ========== */
  23.  
  24. void man_display( t_nshc_calls *nshc_calls, char **text );
  25.  
  26. void man_display( t_nshc_calls *nshc_calls, char **text )
  27. {
  28.     long    i;
  29.     char    *p;
  30.     
  31.     i = SizeResource(text);
  32.     SetHandleSize(text,i+2);
  33.     HLock(text);
  34.     p=*text;
  35.     if ( p[i-1] != '\r' ) {
  36.         p[i]='\r';
  37.         p[i+1]=0;
  38.         }
  39.     else
  40.         p[i]=0;
  41.     nshc_calls->NSH_puts(p);
  42.     HUnlock(text);
  43.     ReleaseResource(text);
  44. }
  45.  
  46. /* ========== find the resource in the current path ========== */
  47. /*
  48.     Construct a name in the format "man parm1 parm2", where parm1
  49.     and parm2 are the parameters given to the "man" command.  If no
  50.     second parameter is given, default to "man parm1 general".
  51. */
  52.  
  53. int man_in_memory(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls);
  54.  
  55. int man_in_memory(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  56. {
  57.     char    **text;
  58.     Str255    rsrcName;
  59.     
  60.     pStrFromC( rsrcName, "man ");
  61.  
  62.     pStrAppendC( rsrcName, &nshc_parms->arg_buf[nshc_parms->argv[1]]);
  63.     pStrAppendC( rsrcName, " " );
  64.  
  65.     if (nshc_parms->argc > 2)
  66.         pStrAppendC( rsrcName, &nshc_parms->arg_buf[nshc_parms->argv[2]] );
  67.     else
  68.         pStrAppendC( rsrcName, "general");
  69.     
  70.     text = (char **)GetNamedResource( 'TEXT', rsrcName);
  71.     
  72.     if (text) {
  73.         man_display( nshc_calls, text );
  74.         return(1);
  75.         }
  76.     else
  77.         return(0);
  78. }
  79.  
  80. /* ========== find the resource in the specified command ========== */
  81.  
  82. int man_in_file(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls);
  83.  
  84. int man_in_file(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  85. {
  86.     char    **text;
  87.     Str255    fileName;
  88.     Str255    rsrcName;
  89.     short    iFileRef;
  90.  
  91.     pStrFromC( fileName, &nshc_parms->arg_buf[nshc_parms->argv[1]] );
  92.     
  93.     iFileRef = -1;
  94.     
  95.     if ( !nshc_calls->NSH_path_which( fileName ) )
  96.         iFileRef = OpenResFile( fileName );
  97.         
  98.     if ( iFileRef < 0 ) {
  99.         nshc_calls->NSH_putStr_err("\pman: Command file not found.\r");
  100.         return(0);
  101.         }
  102.  
  103.     pStrFromC( rsrcName, "man ");
  104.     
  105.     if (nshc_parms->argc > 2)
  106.         pStrAppendC( rsrcName, &nshc_parms->arg_buf[nshc_parms->argv[2]] );
  107.     else
  108.         pStrAppendC( rsrcName, "general");
  109.     
  110.     text = (char **)GetNamedResource( 'TEXT', rsrcName);
  111.     
  112.     if ( !text )
  113.         nshc_calls->NSH_putStr_err("\pman: Manual Page Resource Not Found.\r");
  114.     else
  115.         man_display( nshc_calls, text );
  116.  
  117.     if (iFileRef)
  118.         CloseResFile(iFileRef);
  119.         
  120.     return(text != 0);
  121. }
  122.  
  123. /* ========== the command. ========== */
  124.  
  125. void main(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  126. {
  127.     int    result;
  128.     int    argc;
  129.     
  130.     nshc_parms->action = nsh_idle;        // assume completion
  131.     result = 1;                            // assume failure (resource not found)
  132.  
  133.     if (nshc_bad_version( nshc_parms, nshc_calls, NSHC_VERSION )) return;
  134.  
  135.     argc = nshc_parms->argc;
  136.  
  137.     if ((argc <2) || (argc >3)) {
  138.         nshc_calls->NSH_putStr_err("\pUsage: man command [section].\r");
  139.         result = NSHC_ERR_PARMS;
  140.         }
  141.     else
  142.         if ( man_in_memory( nshc_parms, nshc_calls ) )
  143.             result = NSHC_NO_ERR;
  144.         else
  145.             if ( man_in_file( nshc_parms, nshc_calls ) )
  146.                 result = NSHC_NO_ERR;
  147.         
  148.     nshc_parms->result = result;
  149.     nshc_parms->action = nsh_idle;
  150.     
  151.     ResErr = 0; // reset the global resource error after our searches
  152. }
  153.